﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BSCMFF.Core.DataAccessLayer;
using System.IO;
using System.Drawing;
using System.Drawing.Imaging;

namespace BSCMFF.Web.Pages
{
    public partial class UserInfo : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            BSCEntities context = new BSCEntities();
            User u = (from user in context.Users
                      where user.Login == User.Identity.Name
                      select user).Single();
            
            //MemoryStream ms = new MemoryStream();
            //ms.Write(u.Photo, 0, u.Photo.Length);
            //Bitmap bit = new Bitmap(ms);
            //Response.ContentType = "image/jpeg";//or you can select your imagetype from database or directly write it here
            //bit.Save(Response.OutputStream, ImageFormat.Jpeg);
            //bit.Save(Response.OutputStream);


            //Response.ContentType ="image/jpeg";
            //  Response.Expires = 0; Response.Buffer =true;
            //  Response.Clear();
            //  Response.BinaryWrite(u.Photo); 
            //  Response.End();
            //string strURL ="~/ViewImage.aspx?ID=1" ;
            //ImagePhoto.ImageUrl = strURL;
            RadBinaryImage1.DataValue = u.Photo;
        }

        

        protected void RadButtonUploadImage_Click(object sender, EventArgs e)
        {
            if (RadUpload1.UploadedFiles.Count == 1)
            {

                BSCEntities context = new BSCEntities();
                User u = (from user in context.Users
                          where user.Login == User.Identity.Name
                          select user).Single();
                long length = RadUpload1.UploadedFiles[0].InputStream.Length;
                //u.Photo = new byte[length];
                byte[] buff = new byte[length];
                RadUpload1.UploadedFiles[0].InputStream.Read(buff, 0, (int)length);
                //RadUploadImage.UploadedFiles[0].

                u.Photo = buff;

                context.SaveChanges();
                RadBinaryImage1.DataValue = u.Photo;
            }
        }

        protected void uploadImage_Click(object sender, EventArgs e)
        {
            if (FileUpload1.HasFile == true)
            {

                BSCEntities context = new BSCEntities();
                User u = (from user in context.Users
                          where user.Login == User.Identity.Name
                          select user).Single();
                u.Photo = FileUpload1.FileBytes;
                //FileUpload1.

                context.SaveChanges();
            }
        }
    }
}